import Epub, { type Book as EpubBook } from 'epubjs'
import { useMessage } from 'naive-ui'

const message = useMessage()
export default async (arrayBuffer: ArrayBuffer) => {
  const book: EpubBook = Epub()
  book.open(arrayBuffer, 'binary')
  // 确保书籍加载完成
  await book.ready
  await book.loaded.resources

  let base64 = ''
  try {
    base64 = await book.archive.getBase64(book.cover)
  } catch (error) {
    message.error('获取封面图片失败')
    throw new Error('获取封面图片失败')
  }
  book.destroy()

  return base64
}
